package lex.mxbeans.ui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lex.mxbeans.utils.ObjectUtils;

public class ObjectInspectorPane extends JPanel {
	private JTextArea textArea;
	private JScrollPane scrollPane;
	private JLabel label;

	public ObjectInspectorPane() {
		super();
		initialize();
	}

	public void setObject(Object obj) {
		String className = "";
		String value = "";
		if (null != obj) {
			className = obj.getClass().getCanonicalName();
			if (null == className)
				className = obj.getClass().getName();
			value = ObjectUtils.toString(obj);
		}
		this.label.setText(className);
		this.textArea.setText(value);
	}

	private void initialize() {
		setLayout(new GridBagLayout());
		final GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.weightx = 1;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		add(getLabel(), gridBagConstraints);
		final GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
		gridBagConstraints_1.insets = new Insets(6, 0, 0, 0);
		gridBagConstraints_1.fill = GridBagConstraints.BOTH;
		gridBagConstraints_1.weighty = 1;
		gridBagConstraints_1.weightx = 1;
		gridBagConstraints_1.gridx = 0;
		gridBagConstraints_1.gridy = 1;
		add(getScrollPane(), gridBagConstraints_1);
	}

	/**
	 * @return
	 */
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel();
			label.setText("New JLabel");
		}
		return label;
	}

	/**
	 * @return
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	/**
	 * @return
	 */
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setEditable(false);
		}
		return textArea;
	}
}
